<?php

namespace App\Libs;

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Response;
use Symfony\Component\HttpFoundation\Response as FoundationResponse;

trait ApiResponse
{
    /**
     * @var int
     */
    protected $statusCode = FoundationResponse::HTTP_OK;

    /**
     * @return mixed
     */
    public function getStatusCode()
    {
        return $this->statusCode;
    }

    /**
     * @param $statusCode
     * @return $this
     */
    public function setStatusCode($statusCode)
    {
        $this->statusCode = $statusCode;
        return $this;
    }

    /**
     * @param $data
     * @param array $header
     * @return mixed
     */
    public function respond($data, $header = [])
    {
        return response()->json($data,$this->statusCode,$header);
    }

    /**
     * @param $status
     * @param array $data
     * @param null $code
     * @return mixed
     */
    public function status(array $data, $code = null){
        if ($code){
            $this->setStatusCode($code);
        }
        $status = [
            'code' => $this->statusCode
        ];
        $data = array_merge($status,$data);

        return $this->respond($data);

    }

    /**
     * @param $data
     * @param string $status
     * @return mixed
     */
    public function success($data){

        return $this->status(compact('data'));
    }

    public function error($message,$code = 422){

        return $this->setStatusCode($code)->status(['message'=>$message]);
    }
}
